﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

using WFL.Windows.Forms;

namespace WFL.Demo.WinForm
{
    public partial class HotKeyForm : Form
    {
        public HotKeyForm()
        {
            InitializeComponent();
        }

        private HotKey.MessageFilter _filter;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            _filter = new HotKey.MessageFilter();
            _filter.HotKeyPressed += new HotKeyPressedEventHandler(_filter_HotKeyPressed);
            Application.AddMessageFilter(_filter);
        }

        private void _filter_HotKeyPressed(object sender, HotKeyPressedEventArgs e)
        {
            MessageBox.Show("线程中的热键：" + e.Id + " " + e.Keys.ToString());
        }

        private void _registerHotKey_T_Click(object sender, EventArgs e)
        {
            bool result = HotKey.RegisterHotKey(IntPtr.Zero, 2, Keys.V | Keys.Alt);
            if (!result)
            {
                MessageBox.Show("设置线程热键失败。");
            }
        }

        private void _unregisterHotKey_T_Click(object sender, EventArgs e)
        {
            bool result = HotKey.UnregisterHotKey(IntPtr.Zero, 2);
            if (!result)
            {
                MessageBox.Show("取消线程热键失败。");
            }
        }

        private void _setActivationHotKey_Click(object sender, EventArgs e)
        {
            int result = ActivationHotKey.Set(Handle, Keys.H | ActivationHotKey.Ext);
            MessageBox.Show(result.ToString());
        }

        private void _removeActivationHotKey_Click(object sender, EventArgs e)
        {
            ActivationHotKey.Remove(Handle);
        }

        private void _getActivationHotKey_Click(object sender, EventArgs e)
        {
            Keys keys = ActivationHotKey.Get(Handle);
            MessageBox.Show(keys.ToString());
        }

        private HotKey _hotKey;

        private void _registerHotKey_Click(object sender, EventArgs e)
        {
            if (_hotKey == null)
            {
                _hotKey = new HotKey(Handle);
                _hotKey.HotKeyPressed += new HotKeyPressedEventHandler(_hotKey_HotKeyPressed);
            }
            bool result = _hotKey.Register(1, HotKey.Win | Keys.H);
            if (!result)
            {
                MessageBox.Show("设置热键失败。");
            }
        }

        private void _hotKey_HotKeyPressed(object sender, HotKeyPressedEventArgs e)
        {
            MessageBox.Show(e.Id + " " + e.Keys.ToString());
        }

        private void _unregisterHotKey_Click(object sender, EventArgs e)
        {
            if (_hotKey != null)
            {
                bool result = _hotKey.Unregister(1);
                if (!result)
                {
                    MessageBox.Show("取消热键失败。");
                }
            }
        }

        private KeyboardLLHook _hook;

        private void _startHook_Click(object sender, EventArgs e)
        {
            if (_hook == null)
            {
                _hook = new KeyboardLLHook();
                _hook.KeyDown += new KeyStateEventHandler(_hook_KeyDown);
            }
            bool result = _hook.Hook();
            if (!result)
            {
                MessageBox.Show("启动键盘钩子失败。");
            }
        }

        private void _hook_KeyDown(object sender, KeyStateEventArgs e)
        {
            _keyPressed.Text = e.Modifiers.ToString() + " " + e.KeyCode.ToString()
                //+ " " + (e.IsKeyDown(Keys.LWin) || e.IsKeyDown(Keys.RWin)).ToString();
                + " " + e.Alt.ToString();
        }

        private void _endHook_Click(object sender, EventArgs e)
        {
            if (_hook != null)
            {
                bool result = _hook.Unhook();
                if (!result)
                {
                    MessageBox.Show("关闭键盘钩子失败。");
                }
            }
        }

        
    }
}
